package br.com.fafica.sisape.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.TextArea;
import javax.swing.JButton;

import br.com.fafica.sisape.control.ControllerPrincipal;
import br.com.fafica.sisape.dao.DAOAlergia;
import br.com.fafica.sisape.model.Alergia;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.ImageIcon;

public class TelaCadastrarAlergias extends JFrame {

	private JPanel contentPane;
	static Integer codAletrar;
	static JTextField txt_alergia;
	static TextArea textReacoes; 

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaCadastrarAlergias frame = new TelaCadastrarAlergias();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaCadastrarAlergias() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 840, 540);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblAlergia = new JLabel("Alergia");
		lblAlergia.setForeground(new Color(0, 0, 128));
		lblAlergia.setFont(new Font("Franklin Gothic Medium Cond", Font.PLAIN, 25));
		lblAlergia.setBounds(82, 38, 112, 34);
		contentPane.add(lblAlergia);
		
		txt_alergia = new JTextField();
		txt_alergia.setFont(new Font("Arial", Font.PLAIN, 14));
		txt_alergia.setColumns(10);
		txt_alergia.setBounds(204, 103, 377, 23);
		contentPane.add(txt_alergia);
		
		JLabel lblAlgicoA = new JLabel("Al\u00E9rgico a:");
		lblAlgicoA.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAlgicoA.setFont(new Font("Arial", Font.PLAIN, 14));
		lblAlgicoA.setBounds(70, 106, 124, 19);
		contentPane.add(lblAlgicoA);
		
		JLabel lblReaes = new JLabel("Rea\u00E7\u00F5es:");
		lblReaes.setHorizontalAlignment(SwingConstants.RIGHT);
		lblReaes.setFont(new Font("Arial", Font.PLAIN, 14));
		lblReaes.setBounds(114, 136, 80, 19);
		contentPane.add(lblReaes);
		
		textReacoes = new TextArea();
		textReacoes.setBounds(204, 137, 380, 195);
		contentPane.add(textReacoes);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nome = txt_alergia.getText();
				String reacoes = textReacoes.getText();
				ControllerPrincipal control = new ControllerPrincipal();
				control.InserirAlergia(nome, reacoes);
				TelaMensagemSalvar telaMensagem = new TelaMensagemSalvar();
				telaMensagem.setLocationRelativeTo(null);
				telaMensagem.setVisible(true);
				txt_alergia.setText("");
				textReacoes.setText("");
			}
		});
		btnSalvar.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnSalvar.setBounds(446, 397, 100, 43);
		contentPane.add(btnSalvar);
		
		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnSair.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnSair.setBounds(556, 397, 100, 43);
		contentPane.add(btnSair);
		
		JButton button_2 = new JButton("Deletar");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nome = txt_alergia.getText();
				String reacoes = textReacoes.getText();
				ControllerPrincipal control = new ControllerPrincipal();
				try {
					control.deletarAlergia(codAletrar, nome, reacoes);
					TelaMensagemDeletar telaMensagemDeletar = new TelaMensagemDeletar();
					telaMensagemDeletar.setLocationRelativeTo(null);
					telaMensagemDeletar.setVisible(true);
					txt_alergia.setText("");
					textReacoes.setText("");
					
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		button_2.setFont(new Font("Tahoma", Font.PLAIN, 16));
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(336, 397, 100, 43);
		contentPane.add(button_2);
		
		JButton btnAlterar = new JButton("Alterar");
		btnAlterar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nome = txt_alergia.getText();
				String reacoes = textReacoes.getText();
				ControllerPrincipal control = new ControllerPrincipal();
				try {
					control.editarAlergia(codAletrar, nome, reacoes);
					TelaMensagemSalvar telaMensagem = new TelaMensagemSalvar();
					telaMensagem.setLocationRelativeTo(null);
					telaMensagem.setVisible(true);
					txt_alergia.setText("");
					textReacoes.setText("");
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		btnAlterar.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnAlterar.setBackground(Color.WHITE);
		btnAlterar.setBounds(224, 397, 100, 43);
		contentPane.add(btnAlterar);
		
		JButton btnListar = new JButton("Listar");
		btnListar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TelaListarAlergias telaLocalizar = new TelaListarAlergias();
				telaLocalizar.setLocationRelativeTo(null);
				telaLocalizar.setVisible(true);
			}
		});
		btnListar.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnListar.setBackground(Color.WHITE);
		btnListar.setBounds(114, 397, 100, 43);
		contentPane.add(btnListar);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon("arquivos\\FundoCadastros.png"));
		label.setBounds(-10, 0, 840, 516);
		contentPane.add(label);
	}

}
